/*
 * Segment.java
 *
 * Created on March 27, 2007, 2:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.db;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import org.restlet.data.MediaType;

/**
 *
 * @author alex
 */
public class EntryMedia extends DBObject implements Modifiable {
   
   int id;
   int feed;
   int entry;
   String name;
   MediaType type;
   Timestamp created;
   Timestamp modified;
   
   /** Creates a new instance of Segment */
   public EntryMedia(DB db,int id,int feed,int entry,String name,MediaType type,Timestamp created,Timestamp modified) {
      super(db);
      this.id = id;
      this.feed = feed;
      this.entry = entry;
      this.name = name;
      this.type = type;
      this.created = created;
      this.modified = modified;
   }

   public int getId() {
      return id;
   }

   public int getFeedId() {
      return feed;
   }
   
   public Feed getFeed() 
      throws SQLException
   {
      return db.getFeed(feed);
   }

   public int getEntryId() {
      return entry;
   }
   
   public Entry getEntry() 
      throws SQLException
   {
      return db.getEntry(entry);
   }

   public String getName() {
      return name;
   }
   
   public void setName(String name)
      throws SQLException
   {
      this.name = name;
      DBConnection dbConnection = db.getConnection();
      PreparedStatement s = null;
      try {
         s = dbConnection.getStatement(DB.STATEMENT_RESOURCE_SET_NAME);
         s.setString(1,name);
         s.setInt(2,id);
         int count = s.executeUpdate();
      } finally {
         db.release(s);
         db.release(dbConnection);
      }
   }
   
   public void setMediaType(MediaType type)
      throws SQLException
   {
      this.type = type;
      DBConnection dbConnection = db.getConnection();
      PreparedStatement s = null;
      try {
         s = dbConnection.getStatement(DB.STATEMENT_RESOURCE_SET_MEDIA_TYPE);
         s.setString(1,type.getName());
         s.setInt(2,id);
         int count = s.executeUpdate();
      } finally {
         db.release(s);
         db.release(dbConnection);
      }
   }
   
   public MediaType getMediaType() {
      return type;
   }

   public Timestamp getCreated()
   {
      return created;
   }

   public Timestamp getEdited()
   {
      return modified;
   }
   
   public void delete()
      throws SQLException
   {
      db.deleteResource(id);
   }
   
   public Timestamp touch()
      throws SQLException
   {
      return touch(new Timestamp((new Date()).getTime()));
   }
   
   Timestamp touch(Timestamp modified)
      throws SQLException
   {
      DBConnection dbConnection = db.getConnection();
      PreparedStatement s = null;
      try {
         s = dbConnection.getStatement(DB.STATEMENT_RESOURCE_UPDATED);
         s.setTimestamp(1,modified);
         s.setInt(2,id);
         int count = s.executeUpdate();
         this.modified = modified;
         return modified;
      } finally {
         db.release(s);
         db.release(dbConnection);
      }
   }
   
   public Timestamp edited() 
      throws SQLException
   {
      Timestamp t = touch();
      db.getJournal().recordModify(feed,entry,id);
      return t;
   }
   
}
